<?php

/*
	Class: Role

	About: Author
		J William McCarthy

	About: License
		<http://communit.as/docs/license>

	About: See Also
		<App_Controller_Action_Abstract>
*/
class RoleController extends App_Controller_Action_Abstract {

	/* Group: Instance Methods */

	/*
		Function: init
			Invoked automatically when an instance is created.
			Initializes the current instance.
			Also initializes the parent object (calls init() on the parent instance).
	*/
    function init() {
        Zend_Loader::loadClass('Roles');
        Zend_Loader::loadClass('RolesIndex');
        parent::init();
    }

	/* Group: Actions */

	/*
		Function: index
			Displays the role administration landing page.
	*/
    function indexAction() {
        $roles_index_table = new RolesIndex();
        $this->view->pagetitle = "Manage Roles";     	
        $this->view->roles = $roles_index_table->fetchAll()->toArray();
    }

	/*
		Function: edit
			Edit the permissions that a particular role has.
	*/
    function editAction() {
    	$roles = new Roles();
        $justroles = array();
        $justroles['-1'] = "none";
    	
         Zend_Loader::loadClass('Zend_Filter_StripTags');
	     $filter = new Zend_Filter_StripTags();
	     
        if ($this->getRequest()->isPost()) {
        	
        	//Fix these up first
        	$shortname = trim($filter->filter($this->_request->getPost('shortname')));        	
        	$description = trim($filter->filter($this->_request->getPost('description')));        	
        	$inherits_id = (int)$this->_request->getPost('inherits_id');
        	$isadmin = (int)$this->_request->getPost('isadmin');  
        	$isguest = (int)$this->_request->getPost('isguest');
        	$isdefault = (int)$this->_request->getPost('isdefault');

        	if ($shortname != '') {
    			$data = array(
    			    'shortname' => $shortname,
    			    'description' => $description,   
    			    'isadmin' => $isadmin,    
	        		'isguest' => $isguest,
	        		'isdefault' => $isdefault, 	 			    
    			);

    			if ($inherits_id != -1) {
        			$data['inherits_id'] = $inherits_id;
        		} else {
        			$data['inherits_id'] = null;
        		}

	    		//If we have an id, this is an update.
	    		$id = (int)$this->_request->getPost('id');
	    		if($id != 0) {
	        		$where = 'id = ' . $id;
					$roles->update($data, $where);
	        	} else {
		        	//We don't, this is an insert.
	        		$roles->insert($data);
	        	}
        	    $this->_redirect('/role/');
        	    return;
            }        	

        } else { 

	        if($this->getRequest()->has('id')){
	        	// this is an edit        	
	        	$this->_smarty->assign('Title', "Edit Role");
	        	$id = (int)$this->_request->getParam('id', 0);
			    if ($id > 0) {		        
			        $this->_smarty->assign('Roles', $roles->fetchRow('id=' . $id)->toArray());
			    }	        
		        $this->_smarty->assign('ButtonText', "Edit");
		        foreach($roles->fetchAll('id <> ' . $id)->toArray() as $role){
	        		$justroles[$role['id']] = $role['shortname'];
	        	}
		        $this->_smarty->assign('AllRoles', $justroles );
	        } else {
	        	$this->_smarty->assign('Title', "Add New Role");
	        	// this is a new record        
		        // set up an "empty" role

		        $emptyrole = array(
			        'id' => null,
			        'shortname' => '',
			        'description' => '',    
			        'isadmin' => '',    
			        'isguest' => '',
			        'isdefault' => '',
		    	);

		    	$this->_smarty->assign('Roles', $emptyrole);
		        // additional view fields required by form	        
		        $this->_smarty->assign('ButtonText', "Add");	        
		        foreach($roles->fetchAll()->toArray() as $role){
	        		$justroles[$role['id']] = $role['shortname'];
	        	}
		        $this->_smarty->assign('AllRoles', $justroles);
	        }
        }
        $this->_smarty->assign('YesNo',
			array(
				'0' => 'No',
				'1' => 'Yes',
			)
		 );
		$this->_smarty->assign('ActiveTab', "admin");
		$this->_smarty->display('Role-edit.tpl');
    }        

	/*
		Function: delete
			Delete a role from the system.
			You cannot delete any of the three default roles (admin, guest, member).
			After a successful deletion, the browser is redirected to '/role/' (currently hardcoded).

		HTTP POST Parameters:
			id - The id of the role to delete.
			del - Value must be yes or this doesn't work.
	*/
    function deleteAction(){
        $this->_smarty->assign('Title', "Delete Role");

        $roles = new Roles();
        if ($this->getRequest()->isPost()) {
            Zend_Loader::loadClass('Zend_Filter_Alpha');
            $filter = new Zend_Filter_Alpha();

            $id = (int)$this->_request->getPost('id');
            $del = $filter->filter($this->_request->getPost('del'));

            if ($del == 'Yes' && $id > 0) {
	            $where = 'id = ' . $id;
				$rows_affected = $roles->delete($where);
	        }
        } else {
            $id = (int)$this->_request->getParam('id');
		    if ($id > 0) {

		        $getroles = $roles->fetchRow('id=' . $id);
		        $this->_smarty->assign('Roles', $getroles->toArray());

		        if ($getroles->id > 0) {
		        	$this->_smarty->assign('ActiveTab', "admin");
                    $this->_smarty->display('Role-delete.tpl');
                    return;
		        }
		    }
        }
   	    $this->_redirect('/role/');
    }
}
